package com.ctjsoft.threadstatus;

/**
 * 测试线程停止--使用标志位
 *
 * @author: liaoyy
 * @date: Created in 2021/11/11 15:51
 */
public class ThreadStopDemo implements Runnable {

    private volatile Boolean flag = true;

    @Override
    public void run() {
        while (flag) {
            System.out.println("Thread is running -- " + Thread.currentThread().getName());
            int i = 0;
            while (i++ < 10) {
                if (!flag) {
                    System.out.println("thread invoke end....");
                    return;
                }
                System.out.println("do something..." + i);
            }
        }
    }

    public void stop() {
        this.flag = false;
    }

    public static void main(String[] args) throws InterruptedException {

        ThreadStopDemo stopDemo = new ThreadStopDemo();
        Thread thread = new Thread(stopDemo);
        thread.start();
        Thread.sleep(1);

        stopDemo.stop();

    }
}
